use std::io::Read;
use std::fs::File;
use serde_derive::Deserialize;
use toml;

/// Config defines server configurations
///
/// # Config contains all configurations
#[derive(Deserialize)]
#[derive(Debug)]
pub struct Config {
    pub server: Server,
}

#[derive(Deserialize)]
#[derive(Debug)]
pub struct Server {
    #[serde(rename="port")]
    pub port: i32,

    #[serde(rename="context-path")]
    pub context_path: String,

    #[serde(rename="version")]
    pub version: String,
}

pub fn load(filepath: &String) -> Config {
    let mut file = match File::open(filepath) {
        Ok(f) => f,
        Err(e) => panic!("File {} not found, exception: {}", filepath, e)
    };

    let mut content = String::new();
    match file.read_to_string(&mut content) {
        Ok(s) => s,
        Err(e) => panic!("File Read error: {}, exception: {}", filepath, e)
    };

    let config = toml::from_str(&content).unwrap();
    return config;
}
